package site.brood.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.brood.common.core.user.LoginUserVO;
import site.brood.common.core.web.AjaxJson;
import site.brood.common.log.service.LogService;
import site.brood.common.security.annotation.InnerAuth;
import site.brood.user.pojo.entity.User;
import site.brood.user.service.IPermissionService;
import site.brood.user.service.IRoleService;
import site.brood.user.service.IUserService;

import java.util.Set;

@Api(tags = "用户CRUD")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private LogService logService;

    /**
     * 内部访问接口
     * 获取当前用户信息
     */
    @InnerAuth
    @PostMapping("/inner_get_by_name")
    public AjaxJson<LoginUserVO> innerGetByName(@RequestParam("username") String username)
    {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (null == user)
        {
            return AjaxJson.errorByMessage("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user.getId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user.getId());
        LoginUserVO sysUserVo = new LoginUserVO();
        BeanUtils.copyProperties(user, sysUserVo);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        return AjaxJson.success(sysUserVo);
    }

    /**
     * 外部访问接口
     * 获取当前用户信息
     */
    @PostMapping("/get_by_name")
    public AjaxJson<LoginUserVO> getByName(@RequestParam("username") String username)
    {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (null == user)
        {
            return AjaxJson.errorByMessage("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user.getId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user.getId());
        LoginUserVO userVo = new LoginUserVO();
        BeanUtils.copyProperties(user, userVo);
        userVo.setRoles(roles);
        userVo.setPermissions(permissions);
        userVo.setPassword("");
        return AjaxJson.success(userVo);
    }
    /**
     * 注册用户信息
     */
    @InnerAuth
    @PostMapping("/add")
    public AjaxJson<User> add(@RequestBody User user)
    {
        String username = user.getUsername();

        if (!userService.checkUsernameValid(username))
        {
            return AjaxJson.errorByMessage("保存用户'" + username + "'失败，注册账号已存在");
        }
        return AjaxJson.createByBoolean(userService.add(user));
    }

    // @ApiOperation("获取验证码")
    // @GetMapping(value = "/getAuthCode")
    // public AjaxJson getAuthCode(String telephone) {
    //     return sysUserService.generateAuthCode(telephone);
    // }
    //
    // @ApiOperation("判断验证码是否正确")
    // @PostMapping(value = "/verifyAuthCode")
    // public AjaxJson updatePassword(String telephone, String authCode) {
    //     return sysUserService.verifyAuthCode(telephone,authCode);
    // }
    //
    // @Trace  //skywalking采集注解
    // @Tags({ @Tag(key = "username", value = "arg[0]"),   //采集第1个输入参数
    //         @Tag(key = "password", value = "arg[1]"),   //采集第2个输入参数
    //         @Tag(key = "channel", value = "arg[2]"),   //采集第3个输入参数
    //         @Tag(key = "output", value = "returnedObj") })  //采集返回值
    // @ApiOperation("登录")
    // @RequestMapping("/login")
    // public AjaxJson login(String username, String password,@RequestParam(defaultValue = "APP") String channel) {
    //     // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
    //     UmsAdmin umsAdmin = sysUserService.selectOne(username, password, channel);
    //     if(ObjectUtils.isEmpty(umsAdmin)){
    //         return AjaxJson.error();
    //     }
    //
    //     // 记录当前登录用户到redis(写token到redis)
    //     // 记录:userid, 登录设备
    //     StpUtil.login(umsAdmin.getId(),channel);
    //     String token = StpUtil.getTokenInfo().tokenValue;
    //     return AjaxJson.success(token);
    // }
    //
    // @Trace  //skywalking采集注解
    // @ApiOperation("用户信息")
    // @SaCheckLogin
    // @RequestMapping("/userinfo")
    // public AjaxJson getUserinfo() {
    //     // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
    //     String userId = StpUtil.getLoginIdAsString();
    //
    //     UmsAdmin umsAdmin = sysUserService.selectOne(userId);
    //     if(ObjectUtils.isEmpty(umsAdmin)){
    //         return AjaxJson.error();
    //     }
    //     return AjaxJson.success(umsAdmin);
    // }
}
